package com.sl.au.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.ExdeLocationItem;
import com.sl.au.entity.ExdeviExdePersonLatestLocation;
import com.sl.au.service.ExdeviExdePersonLatestLocationService;
import com.sl.utils.DataConverter;

@RestController
@RequestMapping(value = "/ui/aftersaleview")
public class AfterSaleViewController extends BaseController{
	
	@Autowired
	ExdeviExdePersonLatestLocationService exdeviExdePersonLatestLocationService;
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = this.createLayoutView("aftersaleview/aftersalevviewindex");
		return modelAndView;
	}
	
	@RequestMapping(value = "getLocation", method = RequestMethod.GET)
	public Map<String, Object> getLocation(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String deptName = request.getParameter("deptName").toString();
		List<ExdeviExdePersonLatestLocation> list = this.exdeviExdePersonLatestLocationService.findByOne(deptName);
		List<ExdeLocationItem> listUnion = new ArrayList<ExdeLocationItem>();
		
		for(ExdeviExdePersonLatestLocation listTemp : list){
			boolean isAddNew = true;
			int nLatitude = listTemp.getnLatitude().intValue();
			int nLogitude = listTemp.getnLogitude().intValue();
			
			for(ExdeLocationItem listTemp2 :listUnion){
				if(listTemp2.getLatitude() == nLatitude && listTemp2.getLongitude() == nLogitude){//地址一样了，融入之前listUnion
					isAddNew = false;
					listTemp2.setLocationMessage(listTemp2.getLocationMessage()+";"+listTemp.getName()+"-"+DataConverter.toDateYMD(listTemp.getCreateTime())+"["+listTemp.getsPlace()+"]");
					listTemp2.setName(listTemp2.getName()+"</br>"+listTemp.getName());
					break;
				}
			}
			if(isAddNew){
				ExdeLocationItem exdeLocationItem = new ExdeLocationItem();
				exdeLocationItem.setLatitude(nLatitude);
				exdeLocationItem.setLongitude(nLogitude);
				exdeLocationItem.setLocationMessage(listTemp.getName()+"-"+DataConverter.toDateYMD(listTemp.getCreateTime())+"["+listTemp.getsPlace()+"]");
				exdeLocationItem.setName(listTemp.getName());
				listUnion.add(exdeLocationItem);
			}
		}
		
		map.put("data", listUnion);
		return map;
	}
}
